<%@page import="multiface.web.util.Paginator"%>
<%@page import="multiface.web.util.Util"%>
<%@page import="multiface.awwadco.jpa.News"%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>

<%if (session.getAttribute("admin") == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.News"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<div class="box">
    <%
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        //Parametri della jsp
        String order = request.getParameter("order");
        String direction = request.getParameter("direction");
        String text = request.getParameter("text");
        String jsp = "list_news.jsp";
        String functionJS = "open";
        String indexs = request.getParameter("index");
        int index = 0;

        //Controlli sui parametri
        if (indexs != null) {
            index = Integer.parseInt(indexs);
        }
        if (direction == null) {
            direction = "ASC";
        }

        //Paginatore
        Paginator paginator = new Paginator();
        paginator.setCSSClass("pager-list");
        paginator.setCSSClassCurrentPage("selected");
        paginator.setCurrentIndex(index);
        paginator.setElementsForPage_fromDB(true);
        paginator.setFunctionJS(functionJS);
        paginator.setPageReturn(jsp);

        //Set Parametri iniziali
        paginator.addParameter("index", index + "");


        List<News> list = new LinkedList<News>();
        if (text == null) {
            if (order == null) {
                Query q = em.createQuery("SELECT p FROM News p ORDER BY p.dataora DESC", News.class);
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            } else {
                paginator.addParameter("order", order);
                if (direction.equals("ASC")) {
                    paginator.addParameter("direction", "DESC");
                    Query q = em.createQuery("SELECT p FROM News p ORDER BY p." + order + " ASC", News.class);
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                } else {
                    paginator.addParameter("direction", "ASC");
                    Query q = em.createQuery("SELECT p FROM News p ORDER BY p." + order + " DESC", News.class);
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                }
            }
        } else {
            paginator.addParameter("text", text);
            if (order == null) {
                Query q = em.createNativeQuery("SELECT p.* FROM News p WHERE (p.titolo LIKE ? OR p.testo LIKE ?) ORDER BY p.dataora DESC", News.class);
                q.setParameter(1, "%" + text + "%");
                q.setParameter(2, "%" + text + "%");
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            } else {
                paginator.addParameter("order", order);
                if (direction.equals("ASC")) {
                    paginator.addParameter("direction", "DESC");
                    Query q = em.createNativeQuery("SELECT p.* FROM News WHERE (p.titolo LIKE ? OR p.testo LIKE ?) p ORDER BY p." + order + " ASC", News.class);
                    q.setParameter(1, "%" + text + "%");
                    q.setParameter(2, "%" + text + "%");
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                } else {
                    paginator.addParameter("direction", "ASC");
                    Query q = em.createNativeQuery("SELECT p.* FROM News WHERE (p.titolo LIKE ? OR p.testo LIKE ?) p ORDER BY p." + order + " DESC", News.class);
                    q.setParameter(1, "%" + text + "%");
                    q.setParameter(2, "%" + text + "%");
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                }
            }
        }


        Iterator i = list.iterator();
        out.print("<table class=\"listing\" border=\"0\">");
        out.print("<tr>");
        out.print("<td>" + paginator.getFunction("order", "titolo", "Title") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "dataora", "Date") + "</td>");
        out.print("<td>Writer</td>");
        out.print("<td colspan=\"1\">Delete</td>");
        out.print("</tr>");
        while (i.hasNext()) {
            News p = (News) i.next();

            out.print("<tr id=\"news_" + p.getIdnews() + "\">");
            out.print("<td class=\"td-name\"><a href=\"javascript:open('mod_news.jsp',{nid:" + p.getIdnews() + "})\">" + p.getTitolo() + "</a></td>");
            out.print("<td class=\"td-dataora\">" + Util.FormatData(p.getDataora()) + "</td>");
            out.print("<td class=\"td-code\">" + p.getUtenteIdutente().getUser() + "</td>");
            out.print("<td class=\"td-fast-setting\"><a title=\"Delete News\" href=\"javascript:del('news'," + p.getIdnews() + ");\"><img id=\"delete_news_" + p.getIdnews() + "\" src=\"../engine/img/delete.png\"></img></a></td>");
            out.print("</tr>");
        }
        out.print("</table>");
        //!!!IMPORTANTE!!!! rittere la direzione in quella della pagina attuale
        paginator.addParameter("direction", direction);
        out.print(paginator);
        em.close();
        //emf.close();
%>

</div>

<script>
    $("tr").hover(
    function(){
        $(this).find(".td-name").append($("<div class=\"tr-edit\">EDIT<div>"));
    },
    function () {
        $(this).find(".td-name .tr-edit").remove();
    }
);
</script>

